﻿using Entity.Enum;
using EntityFrameWorkCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using ShardingCore;
using ShardingCore.Bootstrapers;
using ShardingCore.TableExists;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FrameWorkCore.EFCore
{
    public static class EFCoreService
    {
        public static void ConfigureEFCore(this IServiceCollection services, IConfiguration configuration , DefaultDatabaseType DatabaseType)
        {

            #region DefaultDbContext 后续加字段使用 Add-Migration 然后Script-Migration 而不是 update-database
            services.AddShardingDbContext<DefaultDbContext>()
               .AddEntityConfig(op =>
               {

                    op.CreateShardingTableOnStart = false;
                    op.EnsureCreatedWithOutShardingTable = false;
                   // op.ThrowIfQueryRouteNotMatch = true;

                   op.CreateDataBaseOnlyOnStart = true;

                   op.ShardingRouteConfig();

                   op.UseShardingQuery((conn, builder) =>
                   {
                       if (DatabaseType == DefaultDatabaseType.SqlServer)
                           builder.UseSqlServer(conn).UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking)
                           .ReplaceService<IMigrationsSqlGenerator, ShardingSqlServerMigrationsSqlGenerator<DefaultDbContext>>();


                       if (DatabaseType == DefaultDatabaseType.MySql)
                           builder.UseMySql(conn, MySqlServerVersion.LatestSupportedServerVersion).UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);

                   });
                   op.UseShardingTransaction((conn, builder) =>
                   {
                       if (DatabaseType == DefaultDatabaseType.SqlServer)
                           builder.UseSqlServer(conn).UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);

                       if (DatabaseType == DefaultDatabaseType.MySql)
                           builder.UseMySql(conn, MySqlServerVersion.LatestSupportedServerVersion).UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);


                   });

               }).AddConfig(op =>
               {
                   op.ConfigId = "c1";
                   op.ReplaceTableEnsureManager(sp => new SqlServerTableEnsureManager<DefaultDbContext>());
                   op.AddDefaultDataSource("Default", configuration["Default"]);
                   op.AddExtraDataSource(sp =>
                   {
                       return new Dictionary<string, string>()
                       {
                            {
                                "Default1",
                                configuration["Default1"]
                            },
                            {
                                "Default2",
                                configuration["Default2"]
                            },
                       };
                   });

               }).EnsureConfig();
            #endregion
            var buildServiceProvider = services.BuildServiceProvider();
            ShardingContainer.SetServices(buildServiceProvider);
            ShardingContainer.GetService<IShardingBootstrapper>().Start();

        }
       


    }

  

   
}
